package duilibhx.utils;
import cpp.Lib;
import duilibhx.ui.Control;
import duilibhx.ui.UIBase;
import duilibhx.ui.Window;

/**
 * ...
 * @author wangjian
 */
class PaintManager 
{
	private var duilib_handle:Dynamic;
	private var controlMap:Hash<Control>;
	private function new():Void {
		controlMap = new Hash();
	}
	private function init(_dui_handle:Dynamic):Void {
		duilib_handle = _dui_handle;
	}
	public function needUpdate():Void {
		
	}
	
	public function getPaintWindow():Window {
		return null;
	}
	public function getTooltipWindow():Window {
		return null;
	}
	//查找控件
	public function findControl(ctrlName:String,ctrlClass:Dynamic):Dynamic {
		var _control:Control=controlMap.get(ctrlName);
		if (_control == null) {
			var c_handle:Dynamic = dui_pm_findcontrol(duilib_handle, ctrlName);
			if (c_handle != null) {
				_control=Type.createInstance(ctrlClass, [c_handle]);
				untyped _control.registerHxObj();
				controlMap.set(ctrlName, _control);
				return _control;
			}
			return null;
		}
		return _control;
	}
	public function attachDialog(controlUI:Control):Bool {
		untyped return dui_pm_attachdialog(duilib_handle, controlUI.duilib_handle);
	}
	public function addNotifier(ui:UIBase):Bool {
		untyped return dui_pm_addnotifier(duilib_handle, ui.duilib_handle);
	}
	
	public function messageHandler(msgId:Int, wParam:Int, lParam:Int,?resId:Int=0):Bool {
		return dui_pm_messagehandle(duilib_handle, msgId, wParam, lParam, resId);
	}
	
	public static function getHSL():HSL {
		var hsl:HSL = { h:0, s:0, l:0 };
		dui_pm_gethsl(hsl);
		return hsl;
	}
	public static function setHSL(h:Int,s:Int,l:Int):Void {
		dui_pm_sethsl( h, s, l);
	}
	public function setAlpha(v:Int):Void {
		dui_pm_setalpha(duilib_handle,v);
	}
	public static function setResourcePath(path:String):Void {
		dui_pm_set_resourcepath(path);
	}
	public static function setResourceZip(zipPath:String, isCached:Bool = false):Void {
		
	}
	
	public static function getResourcePath():String {
		return dui_pm_get_resourcepath();
	}
	
	public static function getInstancePath():String {
		return dui_pm_get_instancepath();
	}
	
	public static function reloadSkin():Void {
		dui_pm_reloadskin();
	}
	

	static var dui_pm_get_instancepath = Lib.load("DuiLib", "dui_pm_get_instancepath", 0);
	static var dui_pm_get_resourcepath = Lib.load("DuiLib", "dui_pm_get_resourcepath", 0);
	static var dui_pm_set_resourcepath = Lib.load("DuiLib", "dui_pm_set_resourcepath", 1);
	static var dui_pm_reloadskin = Lib.load("DuiLib", "dui_pm_reloadskin", 0);
	
	//static var dui_pm_init = Lib.load("DuiLib", "dui_pm_init", 2);
	static var dui_pm_attachdialog = Lib.load("DuiLib", "dui_pm_attachdialog", 2);
	static var dui_pm_addnotifier = Lib.load("DuiLib", "dui_pm_addnotifier", 2);
	static var dui_pm_messagehandle = Lib.load("DuiLib", "dui_pm_messagehandle", 5);
	static var dui_pm_findcontrol = Lib.load("DuiLib", "dui_pm_findcontrol", 2);
	static var dui_pm_gethsl = Lib.load("DuiLib", "dui_pm_gethsl", 1);
	static var dui_pm_sethsl = Lib.load("DuiLib", "dui_pm_sethsl", 3);
	static var dui_pm_setalpha=Lib.load("DuiLib", "dui_pm_setalpha", 2);
}

/*
class UILIB_API CPaintManagerUI
{
public:
    CPaintManagerUI();
    ~CPaintManagerUI();

public:
    void Init(HWND hWnd);
    void NeedUpdate();
    void Invalidate(RECT& rcItem);

    HDC GetPaintDC() const;
    HWND GetPaintWindow() const;
    HWND GetTooltipWindow() const;

    POINT GetMousePos() const;
    SIZE GetClientSize() const;
    SIZE GetInitSize();
    void SetInitSize(int cx, int cy);
    RECT& GetSizeBox();
    void SetSizeBox(RECT& rcSizeBox);
    RECT& GetCaptionRect();
    void SetCaptionRect(RECT& rcCaption);
    SIZE GetRoundCorner() const;
    void SetRoundCorner(int cx, int cy);
    SIZE GetMinInfo() const;
    void SetMinInfo(int cx, int cy);
    SIZE GetMaxInfo() const;
    void SetMaxInfo(int cx, int cy);
    void SetTransparent(int nOpacity);
    void SetBackgroundTransparent(bool bTrans);
    bool IsShowUpdateRect() const;
    void SetShowUpdateRect(bool show);

    static HINSTANCE GetInstance();
    static CStdString GetInstancePath();
    static CStdString GetCurrentPath();
    static HINSTANCE GetResourceDll();
    static const CStdString& GetResourcePath();
    static const CStdString& GetResourceZip();
    static bool IsCachedResourceZip();
    static HANDLE GetResourceZipHandle();
    static void SetInstance(HINSTANCE hInst);
    static void SetCurrentPath(LPCTSTR pStrPath);
    static void SetResourceDll(HINSTANCE hInst);
    static void SetResourcePath(LPCTSTR pStrPath);
	static void SetResourceZip(LPVOID pVoid, unsigned int len);
    static void SetResourceZip(LPCTSTR pstrZip, bool bCachedResourceZip = false);
    static void GetHSL(short* H, short* S, short* L);
    static void SetHSL(bool bUseHSL, short H, short S, short L); // H:0~360, S:0~200, L:0~200 
    static void ReloadSkin();
    static bool LoadPlugin(LPCTSTR pstrModuleName);
    static CStdPtrArray* GetPlugins();

    bool UseParentResource(CPaintManagerUI* pm);
    CPaintManagerUI* GetParentResource() const;

    DWORD GetDefaultDisabledColor() const;
    void SetDefaultDisabledColor(DWORD dwColor);
    DWORD GetDefaultFontColor() const;
    void SetDefaultFontColor(DWORD dwColor);
    DWORD GetDefaultLinkFontColor() const;
    void SetDefaultLinkFontColor(DWORD dwColor);
    DWORD GetDefaultLinkHoverFontColor() const;
    void SetDefaultLinkHoverFontColor(DWORD dwColor);
    DWORD GetDefaultSelectedBkColor() const;
    void SetDefaultSelectedBkColor(DWORD dwColor);
    TFontInfo* GetDefaultFontInfo();
    void SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
    DWORD GetCustomFontCount() const;
    HFONT AddFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
    HFONT AddFontAt(int index, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
    HFONT GetFont(int index);
    HFONT GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
    bool FindFont(HFONT hFont);
    bool FindFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
    int GetFontIndex(HFONT hFont);
    int GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
    bool RemoveFont(HFONT hFont);
    bool RemoveFontAt(int index);
    void RemoveAllFonts();
    TFontInfo* GetFontInfo(int index);
    TFontInfo* GetFontInfo(HFONT hFont);

    const TImageInfo* GetImage(LPCTSTR bitmap);
    const TImageInfo* GetImageEx(LPCTSTR bitmap, LPCTSTR type = NULL, DWORD mask = 0);
    const TImageInfo* AddImage(LPCTSTR bitmap, LPCTSTR type = NULL, DWORD mask = 0);
    const TImageInfo* AddImage(LPCTSTR bitmap, HBITMAP hBitmap, int iWidth, int iHeight, bool bAlpha);
    bool RemoveImage(LPCTSTR bitmap);
    void RemoveAllImages();
    void ReloadAllImages();

    void AddDefaultAttributeList(LPCTSTR pStrControlName, LPCTSTR pStrControlAttrList);
    LPCTSTR GetDefaultAttributeList(LPCTSTR pStrControlName) const;
    bool RemoveDefaultAttributeList(LPCTSTR pStrControlName);
    const CStdStringPtrMap& GetDefaultAttribultes() const;
    void RemoveAllDefaultAttributeList();

    bool AttachDialog(CControlUI* pControl);
    bool InitControls(CControlUI* pControl, CControlUI* pParent = NULL);
    void ReapObjects(CControlUI* pControl);

    bool AddOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl);
    CStdPtrArray* GetOptionGroup(LPCTSTR pStrGroupName);
    void RemoveOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl);
    void RemoveAllOptionGroups();

    CControlUI* GetFocus() const;
    void SetFocus(CControlUI* pControl);
    void SetFocusNeeded(CControlUI* pControl);

    bool SetNextTabControl(bool bForward = true);

    bool SetTimer(CControlUI* pControl, UINT nTimerID, UINT uElapse);
    bool KillTimer(CControlUI* pControl, UINT nTimerID);
    void KillTimer(CControlUI* pControl);
    void RemoveAllTimers();

    void SetCapture();
    void ReleaseCapture();
    bool IsCaptured();

    bool AddNotifier(INotifyUI* pControl);
    bool RemoveNotifier(INotifyUI* pControl);   
    void SendNotify(TNotifyUI& Msg, bool bAsync = false);
    void SendNotify(CControlUI* pControl, LPCTSTR pstrMessage, WPARAM wParam = 0, LPARAM lParam = 0, bool bAsync = false);

    bool AddPreMessageFilter(IMessageFilterUI* pFilter);
    bool RemovePreMessageFilter(IMessageFilterUI* pFilter);

    bool AddMessageFilter(IMessageFilterUI* pFilter);
    bool RemoveMessageFilter(IMessageFilterUI* pFilter);

    int GetPostPaintCount() const;
    bool AddPostPaint(CControlUI* pControl);
    bool RemovePostPaint(CControlUI* pControl);
    bool SetPostPaintIndex(CControlUI* pControl, int iIndex);

    void AddDelayedCleanup(CControlUI* pControl);

    CControlUI* GetRoot() const;
    CControlUI* FindControl(POINT pt) const;
    CControlUI* FindControl(LPCTSTR pstrName) const;
    CControlUI* FindSubControlByPoint(CControlUI* pParent, POINT pt) const;
    CControlUI* FindSubControlByName(CControlUI* pParent, LPCTSTR pstrName) const;
    CControlUI* FindSubControlByClass(CControlUI* pParent, LPCTSTR pstrClass, int iIndex = 0);
    CStdPtrArray* FindSubControlsByClass(CControlUI* pParent, LPCTSTR pstrClass);
    CStdPtrArray* GetSubControlsByClass();

    static void MessageLoop();
    static bool TranslateMessage(const LPMSG pMsg);
	static void Term();

    bool MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes);
    bool PreMessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes);
};

} // namespace DuiLib

#endif // __UIMANAGER_H__
*/